#include "device.h"
#include "modbus.h"
#include <QDebug>
extern QByteArray recvDataSave;
extern QByteArray sendDataSave;
extern int bufDataCount;        //接收到的数据内容的字节数
extern QMutex Locker;
extern DockWidgets dockWidgets;
Device::Device()
{
    SlotsInit();
    mThread.start();
    this->moveToThread(&mThread);
    CreateDevice();
    modbus->ConnectSignalSlots();
    SlotsInitWhenDeviceCreated();
}

Device::~Device()
{
    mThread.quit();
    mThread.wait();
}

void Device::SlotsInit()
{
    connect(this,SIGNAL(To_CreateDevice()),this,SLOT(Slots_CreateDevice()),Qt::BlockingQueuedConnection);
}

void Device::SlotsInitWhenDeviceCreated()
{
    connect(timer,SIGNAL(timeout()),this,SLOT(Slots_TimeOut()));
    connect(this,SIGNAL(To_StartTimer()),this,SLOT(Slots_StartTimer()));
    connect(this,SIGNAL(To_StopTimer()),this,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(To_OpenDevice()),this,SLOT(Slots_OpenDevice()),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(To_WriteData(QByteArray,int)),this,SLOT(WriteReadFunction(QByteArray,int)));

}

void Device::CreateDevice()
{
    emit To_CreateDevice();
}

void Device::OpenDevice()
{
    emit To_OpenDevice();
}

void Device::StartTimer()
{
    emit To_StartTimer();
}

void Device::StopTimer()
{
    emit To_StopTimer();
}

DockWidget2* Device::GetDockWidget()
{
    DockWidget2* widgetPtr=nullptr;
    if(frameIndex<dockWidgets.currentIDVector.count())
    {
        widgetPtr=dockWidgets.element[dockWidgets.currentIDVector[frameIndex]].widgetPtr;
        frameIndex++;
        return  widgetPtr;
    }
    else
    {
        frameIndex=0;
        if(dockWidgets.currentIDVector.count()<1)
        {
            return nullptr;
        }
        else
        {
            widgetPtr=dockWidgets.element[frameIndex].widgetPtr;
            frameIndex++;
            return widgetPtr;
        }
    }
}

void Device::Slots_CreateDevice()
{
    qDebug()<<__FUNCTION__;
    modbus=new Modbus();
    timer=new QTimer();
}

void Device::Slots_TimeOut()
{
    qDebug()<<__FUNCTION__<<"in";
    DockWidget2 *dockWidgetPtr=nullptr;
    dockWidgetPtr=GetDockWidget();

    if(dockWidgetPtr!=nullptr)
    {
        if(dockWidgetPtr->type==1)
            modbus->SendFc3(deviceAddress,dockWidgetPtr->registerStart,dockWidgetPtr->registerSum,recvBuf.recvBuf);
        else if(dockWidgetPtr->type==2)
            modbus->SendFc4(deviceAddress,dockWidgetPtr->registerStart,dockWidgetPtr->registerSum,recvBuf.recvBuf);
        qDebug()<<"发送完查询命令:"<<dockWidgetPtr->registerStart<<dockWidgetPtr->registerSum;
        dockWidgetPtr->UpdateSendFrame();
        if(bufDataCount==dockWidgetPtr->registerSum*2)
        {
            dockWidgetPtr->UpdateRecvFrame();
            dockWidgetPtr->timeout=timeout;
        }

        recvBuf.bufDataCount=bufDataCount;
        qDebug()<<u8"发送寄存器的值:"<<dockWidgetPtr->registerStart<<dockWidgetPtr->registerSum<<u8"收到的字节数:"<<bufDataCount;
        emit dockWidgetPtr->To_ShowRecv(recvBuf,sendDataSave,recvDataSave,frameCount);
        qDebug()<<u8"发送信号的编号:"<<frameCount;
        frameCount++;
    }

    qDebug()<<__FUNCTION__<<"out";
}
void Device::Slots_StartTimer()
{
    timer->start(timeout);
    qDebug()<<"device timer is start";
}

void Device::Slots_OpenDevice()
{
    modbus->port.open(QSerialPort::ReadWrite);
    qDebug()<<"open device"<<modbus->port.isOpen();
}

void Device::Slots_StopTimer()
{
    timer->stop();
    qDebug()<<"stop";
}

void Device::WriteReadFunction(QByteArray sendData, int timeouts)
{
    QByteArray recvData;
    modbus->SendReadFunction(sendData,timeouts,recvData);
    emit To_ShowRecvData(recvData);
}
